<?php 
	require_once('header_sidebar.php');
	require_once('Class/connexionDB.Class.php');
	require_once('Class/displayInHtml.Class.php');
	require_once('Class/session.Class.php');
	
	$session 		= session::getInstance();
	$db 			= new connexionDB();
	//$mail 			= $db->getMailAdress();
	$mail_adress	= $db->getMailAdress();
	$groups     	= $db->getGroupForIdAccount($session->id_account);
	$mail_content 	= $db->getSavedMail();

	$hours 			= array('0' => '00:00',
							'1' => '01:00',
							'2' => '02:00',
							'3' => '03:00',
							'4' => '04:00',
							'5' => '05:00',
							'6' => '06:00',
							'7' => '07:00',
							'8' => '08:00',
							'9' => '09:00',
							'10' => '10:00',
							'11' => '11:00',
							'12' => '12:00',
							'13' => '13:00',
							'14' => '14:00',
							'15' => '15:00',
							'16' => '16:00',
							'17' => '17:00',
							'18' => '18:00',
							'19' => '19:00',
							'20' => '20:00',
							'21' => '21:00',
							'22' => '22:00',
							'23' => '23:00');

	$days 			= array('all' 	=> $translate->translate('weekly'),
							'1' 	=> $translate->translate('mon'),
							'2' 	=> $translate->translate('tue'),
							'3' 	=> $translate->translate('wed'),
							'4' 	=> $translate->translate('thu'),
							'5' 	=> $translate->translate('fri'));

	$months			= array('all'	=> $translate->translate('monthly'),
							'1' 	=> $translate->translate('jan'),
							'2' 	=> $translate->translate('feb'),
							'3' 	=> $translate->translate('mar'),
							'4' 	=> $translate->translate('apr'),
							'5' 	=> $translate->translate('may'),
							'6' 	=> $translate->translate('jun'),
							'7' 	=> $translate->translate('jul'),
							'8' 	=> $translate->translate('aug'),
							'9' 	=> $translate->translate('sep'),
							'10' 	=> $translate->translate('oct'),
							'11' 	=> $translate->translate('nov'),
							'12' 	=> $translate->translate('dec'));
?>
	<link rel="stylesheet" href="js/plugins/chosen/chosen.min.css">
	 <aside class="right-side">
		<section class="content-header">
		    <h1>
		        <?php echo $translate->translate('mail'); ?>
		        <small><?php echo $translate->translate('config'); ?></small>
		    </h1>
		    <ol class="breadcrumb">
		        <li><a href="#"><i class="fa fa-dashboard"></i><?php echo $translate->translate('mail'); ?></a></li>
		        <li class="active"><?php echo $translate->translate('config'); ?></li>
		    </ol>
		</section>
		<?php if (isset($session->name_accountType) && $session->name_accountType == "admin") : ?>
			<section class="content">
				<div class="col-md-12">
					<div class='box'>
						<div>
			                <div class='box-header'>
			                    <h3 class='box-title'><?php echo $translate->translate('mail_body'); ?> : </h3>
			                </div>
			                <div class='box-body pad'>
		                        <div id="recipient" class="form-group">
		                        	<div class="input-group">
		                        		<span class="input-group-addon"><i class="fa fa-envelope"></i></span>
									    <select id="recipient_mail" name="recipient_mail" multiple="" class="chosen-select1" id="form-field-select-4" data-placeholder="<?php echo $translate->translate('selectRecipient') . ' :'; ?>">
									    	<?php foreach ($mail_adress as $value) : ?>
									    		<?php 
									    			$selected = '';
									    			if ($value['active'] == 1)
									    			{
									    				$selected = 'selected';
									    			}
									    		?>
									        	<option <?php echo $selected; ?> value="<?php echo $value['mail']; ?>"><?php echo $value['mail']; ?></option>
									        <?php endforeach; ?>
									    </select>
									</div>
								</div>
								<div id="recipient_option" class="form-group">
		                        	<div class="input-group">
		                        		<span class="input-group-addon"><i class="fa fa-envelope"></i></span>
		                        		<input type="text" class="form-control" id='recipient_mail_bis'>
		                        	</div>
		                        </div>
		                        <div class="form-group">
		                            <input type="text" class="form-control" id="subject" name="subject" placeholder="<?php echo $translate->translate('mailSubject') . ' :'; ?>" value="<?php echo $mail_content['mail_subject']; ?>">
		                        </div>
		                        <textarea id="mail_content" name="mail_content" class="textarea" style="width: 100%; height: 200px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;">
		                        	<?php echo $mail_content['mail_body']; ?>
		                        </textarea>
		                        <div class="box-footer">
		                            <button  id="save_mail_type" type="submit" class="btn btn-danger"><?php echo $translate->translate('save'); ?></button>
		                        </div>
			                </div>
			            </div>
			        </div>
		        </div>
			</section>
		<?php endif;?>
	</aside>
</div>
<?php require_once('footer.php');?>
<script src="js/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js" type="text/javascript"></script>
<!-- Chosen pluggin -->
<script src="js/plugins/chosen/chosen.jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function() {
        $(".textarea").wysihtml5();
	    $(".chosen-select1").chosen();
    });
   	$(".chosen-select1").chosen({ width:"100%" });
	
    var config = {
      '.chosen-select'           : {}
    }
    for (var selector in config) {
      $(selector).chosen(config[selector]);
    }

    $('#group').change(function(){
    	var idGroup = $('#group option:selected').val();
    
	    $.ajax({
	            type: "GET",
	            url: "get_associated_email.php",
	            data: {
	                    id_group 	: idGroup,
	                    type 		: 'get_mail'
	                }
	            }).success(function(result) {
	            	$('#associated_mail').val(result);
	            });
    });

    $('#updated_mail').click(function(){
    	var mailInput 	= $('#associated_mail').val();
    	var idGroup 	= $('#group option:selected').val();

    	if (mailInput != '')
    	{
    		$.ajax({
	            type: "GET",
	            url: "get_associated_email.php",
	            data: {
	                    mail_content : mailInput,
	                    id_group	 : idGroup,
	                    type 		 : 'up_mail'
	                }
	            }).success(function(result) {
	            	console.log(result);
	            });
	            $('#div_mail').removeClass('has-error');
    	}
    	else
    	{
    		$('#div_mail').addClass('has-error');
    	}
    	
    });


    $('#save_mail_type').click(function(){
    	var sendTo 			= [];
    	var newSubject 		= $('#subject').val();
    	var mailContent		= $('#mail_content').val();
    	var sendToBis 		= $('#recipient_mail_bis').val();

    	
    	$('#recipient_option').removeClass('has-error');

    	if ($('#recipient_mail').chosen().val() == null && sendToBis == '')
		{
			$('#recipient_option').addClass('has-error');
		}
		else
		{
			if ($('#recipient_mail').chosen().val() != null)
			{
				sendTo = $.merge(sendTo, $('#recipient_mail').chosen().val());	
			}
			
			sendTo.push(sendToBis);
			$.ajax({
				type 	: "GET",
				url 	: "get_associated_email.php",
				data 	: {
							send_to 		: sendTo,
							new_subject		: newSubject,
							mail_content 	: mailContent,
							type 			: 'up_mail_content'
						}
			}).success(function(result) {
				location.reload(true); 
			});
		}
    });
</script>